//
//  CGFileEvaluateViewController.m
//  SchoolClient
//
//  Created by mac on 15/11/18.
//  Copyright © 2015年 mac. All rights reserved.


#import "CGFileEvaluateViewController.h"
#import "CGFileEvaluateDetailViewController.h"

@interface CGFileEvaluateViewController ()
@property(nonatomic,strong)NSMutableArray *files;
@end

@implementation CGFileEvaluateViewController

-(NSMutableArray *)files{
    if (!_files) {
        _files = [NSMutableArray array];
    }
    return _files;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.title = @"职称评定";
    [self initTableView];
    
    // 添加下拉刷新控件
    [self.tableView addHeaderWithTarget:self action:@selector(loadNewStatus)];
    
    // 自动下拉刷新
    [self.tableView headerBeginRefreshing];
    
    //使cell的线条充满
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
}

#pragma mark 下拉刷新
-(void)loadNewStatus{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"pageSize"] = [NSString stringWithFormat:@"%d",(int)SCREEN_HEIGHT/100];
    
    if (!self.files.count) {
        parameters[@"page"] = @"0";   //第一次加载最新的数据
    }else{
        CGFileService *fileReport = [self.files firstObject];
        parameters[@"id"] = [NSString stringWithFormat:@"%d",fileReport.file_id];
    }
    
    //检查网络是否可用
    if (![ZYHRequest NetWorkIsOK]) {
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络不可用"];
        // 结束下拉刷新
        [self.tableView headerEndRefreshing];
        return;
    }
    
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/files/getTitleEvaluation.app" parameters:parameters success:^(id responseData) {
        
        NSLog(@"档案报导请求成功!!");
        // 结束下拉刷新
        [self.tableView headerEndRefreshing];
        //把接收到的数组对象变成model
        
        NSDictionary *jsonDic =  [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        NSString *value = jsonDic[@"data"];
        
        if (![value isEqualToString:@"null"]) {
            NSData *valueData = [value dataUsingEncoding:NSUTF8StringEncoding];
            NSArray *fileArr = [CGFileService objectArrayWithJSONData:valueData];
            // 把最新的招聘信息插入到最前面
            NSIndexSet *fileIndex = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, fileArr.count)];
            [self.files insertObjects:fileArr atIndexes:fileIndex];
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
        return;
    }];
}

#pragma mark overide 表格的代理方法实现
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.files.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"myCell";
    
    CGFileReportCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[CGFileReportCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    //给单元格属性赋值
    cell.fileReport = self.files[indexPath.row];
    return cell;
    
}

//设置单元格高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CGFileService *file = self.files[indexPath.row];
    CGFileEvaluateDetailViewController *detailVC = [CGFileEvaluateDetailViewController new];
    detailVC.currentFile = file;
    [self.navigationController pushViewController:detailVC animated:YES];
}

//使cell的线条充满
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}


@end
